package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import cn.b.sky.mes.mm.enums.MoveDirectionECode;
import cn.b.sky.mes.mm.model.InventoryMove;
import cn.b.sky.mes.mm.model.MoveType;
import cn.b.sky.mes.mm.model.StorageLocation;
import cn.b.sky.mes.mm.model.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.service.InventoryService;
import cn.b.sky.mes.mm.service.StorageLocationService;
import cn.b.sky.mes.mm.service.WarehouseService;
import cn.b.sky.common.model.DWZResult;

import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/moveStock/*")
public class MoveStockController extends SecurityController {

  private static final String MOVE_STOCK_PAGE = "mm/moveStock/moveStock";

  @Autowired
  InventoryService inventoryService;

  @Autowired
  StorageLocationService storageLocationService;

  @Autowired
  WarehouseService warehouseService;

  @RequestMapping(value = "moveStock")
  public String moveStock(Model model, String navTabId) {
    model.addAttribute("navTabId", navTabId);
    List<Warehouse> warehouses = inventoryService.selectUserWarehouseList(getSiteCode(),
        SecurityContextUtil.getCurrentUserName(), MoveDirectionECode.OUTIN.getValue());
    model.addAttribute("warehouses", warehouses);
    return MOVE_STOCK_PAGE;
  }

  @RequestMapping(value = "getStorageLocations")
  @ResponseBody
  public DWZResult getStorageLocations(String warehousCode) {
    Warehouse warehouse = warehouseService.getWarehouse(warehousCode, getSiteCode());
    List<StorageLocation> locations = null;
    if (warehouse.getLocationManageFlag().intValue() == 1) {
      locations = storageLocationService.selectStorageLocationByWarehouse(getSiteCode(), warehousCode);
    }
    return ajaxDone(locations);
  }

  @RequestMapping(value = "getCommonMoveTypes")
  @ResponseBody
  public DWZResult getCommonMoveTypes(String outWarehousCode, String inWarehousCode) {
    List<MoveType> typesForOut = inventoryService.selectMoveTypeByDirection(getSiteCode(),
        SecurityContextUtil.getCurrentUserName(), outWarehousCode, MoveDirectionECode.OUTIN.getValue());
    List<MoveType> typesForIn = inventoryService.selectMoveTypeByDirection(getSiteCode(),
        SecurityContextUtil.getCurrentUserName(), outWarehousCode, MoveDirectionECode.OUTIN.getValue());
    List<MoveType> types = new ArrayList<MoveType>();
    for (MoveType moveTypeO : typesForOut) {
      for (MoveType moveTypeI : typesForIn) {
        if (moveTypeO.getMoveTypeCode().equals(moveTypeI.getMoveTypeCode())) {
          types.add(moveTypeO);
          break;
        }
      }
    }
    return ajaxDone(types);
  }

  @RequestMapping(value = "saveMoveStock")
  @ResponseBody
  public DWZResult saveMoveStock(InventoryMove inventoryMove) {
    if (inventoryMove == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    setCreateInfo(inventoryMove);
    inventoryMove.setSiteCode(getSiteCode());
    inventoryService.doMoveStock(inventoryMove);

    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);

  }
}
